﻿using MediatR;
using W.Warehouse.Api.Application.Command.MoveCommand;
using W.Warehouse.Api.Application.Command.StorageCommand;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.MoveCommandHadler
{
    public class MoveDelectRangCommandHadler : IRequestHandler<MoveDelectRangCommand, ApiResult<int>>
    {
        private readonly IMoveProductRepository moveProductRepository;
        private readonly IMoveTheLibraryRepository moveTheLibraryRepository;
        public MoveDelectRangCommandHadler(IMoveProductRepository moveProductRepository, IMoveTheLibraryRepository moveTheLibraryRepository)
        {
            //产品信息
            this.moveProductRepository = moveProductRepository;
            //管理表
            this.moveTheLibraryRepository = moveTheLibraryRepository;
        }

        public async Task<ApiResult<int>> Handle(MoveDelectRangCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var list = await moveProductRepository.GetAllAsync();
                var plist=list.Where(c=>request.MoveProductId.Contains
                (c.MoveProductId)).ToList();
                foreach (var item in plist)
                {
                    item.IsDelete = true;
                    item.DeleteTime = DateTime.Now;
                }
                result.Code=await moveProductRepository.UpdateRangeAsync(plist);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
